package com.bleeth.flow.web.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.sax.Excel03SaxReader;
import cn.hutool.poi.excel.sax.Excel07SaxReader;
import cn.hutool.poi.excel.sax.ExcelSaxReader;

import java.util.List;

/**
 * @author ：Bleeth
 * @date ：2022-05-16
 * @description：excel工具
 */
public class ExcelUtil {

    public static Tuple excelReader(String excelPath) {
        List<List<Object>> lineList = CollUtil.newArrayList();
        List<List<Object>> header = CollUtil.newArrayList();

        ExcelSaxReader reader = null;
        if (StrUtil.endWith(excelPath, ".xlsx")) {
            reader = new Excel07SaxReader((int sheetIndex, long rowIndex, List<Object> rowList) -> {
                //过滤第一行
                if (rowIndex == 0) {
                    header.add(rowList);
                } else {
                    lineList.add(rowList);
                }
            });
        } else {
            reader = new Excel03SaxReader((int sheetIndex, long rowIndex, List<Object> rowList) -> {
                //过滤第一行
                if (rowIndex == 0) {
                    header.add(rowList);
                } else {
                    lineList.add(rowList);
                }
            });
        }

        reader.read(excelPath,0);
        Tuple tuple = new Tuple(header.get(0), lineList);
        System.err.println(excelPath + "读数据完成，读数量为：" + lineList.size());
        return tuple;
    }

}
